<?php

// +----------------------------------------------------------------------
// | 零云 [ 简单 高效 卓越 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.lingyun.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com>
// +----------------------------------------------------------------------
namespace Home\Controller;
use Think\Page;
use Common\Controller\ControllerController;

ini_set('date.timezone','Asia/Shanghai');
/**
 * 导航控制器
 * @author jry <598821125@qq.com>
 */
class NavController extends HomeController
{
    /**
     * 默认方法
     * @author jry <598821125@qq.com>
     */
    public function index()
    {
        $this->assign('meta_title', "首页");
        $this->display();
    }

    /**
     * 单页类型
     * @author jry <598821125@qq.com>
     */
    public function page($id)
    {
        $nav_object    = D('Admin/Nav');
        $con['id']     = $id;
        $con['status'] = 1;
        $info          = $nav_object->where($con)->find();
        $this->assign('info', $info);
        $this->assign('meta_title', $info['title']);
        $this->display();    
    }

    /**
     * 文章列表
     * @author jry <598821125@qq.com>
     */
    public function lists($id)
    {
        $nav_object    = D('Admin/Nav');
        $con['id']     = $id;
        $con['status'] = 1;
        $info          = $nav_object->where($con)->find();

        // 文章列表
        $map['status'] = 1;
        $map['cid']    = $id;
        $p             = $_GET["p"] ?: 1;
        $post_object   = D('Admin/Post');
        $data_list     = $post_object
            ->where($map)
            ->page($p, C("ADMIN_PAGE_ROWS"))
            ->order("sort desc,id desc")
            ->select();
        $page = new Page(
            $post_object->where($map)->count(),
            C("ADMIN_PAGE_ROWS")
        );

        $this->assign('data_list', $data_list);
        $this->assign('page', $page->show());
        $this->assign('meta_title', $info['title']);
        $this->display();
    }

    /**
     * 文章详情
     * @author jry <598821125@qq.com>
     */
    public function post($id)
    {
        $post_object   = D('Admin/Post');
        $con['id']     = $id;
        $con['status'] = 1;
        $info          = $post_object->where($con)->find();
        if ($info['kolid']>0 && $info['cid']!=22) {
            $map['kolid'] = $info['kolid'];
            $map['cid'] = 22;
            $kol_info = $post_object->where($map)->find();
            $info['lead_url'] = $kol_info['cover_url'];
            $info['leadintro'] = $kol_info['abstract'];
            $info['leadmemo'] = $kol_info['memo'];
            $info['kolid'] = $kol_info['id'];
        }
        // 阅读量加1
        $result = $post_object->where(array('id' => $id))->SetInc('view_count');

        $this->assign('info', $info);
        
        $this->assign('meta_title', $info['title']);
        if ($info['cid']==22) {
            $this->display('kolpage');
        }else{
            $this->display('page');
        }
    }

    /**
     * 文章详情
     * @author jry <598821125@qq.com>
     */
    //public function order($id)
    public function order()
    {
        /*
        $post_object   = D('Admin/Post');
        $con['id']     = $id;
        $con['status'] = 1;
        $info          = $post_object->field('id,title')->where($con)->find();

        if (!$info['id']) {
            $this->error('信息不存在');
        }
        
        $this->assign('info', $info);
        */
        $this->assign('meta_title', $info['title']);
        $this->display('order');
    }

    public function checkTime($atime) {
        $i_time = explode(",",$atime);
        $max_pos= array_search(max($i_time), $i_time);
        $max_time  = $i_time[$max_pos];
        $min_pos = array_search(min($i_time), $i_time);
        $min_time  = $i_time[$min_pos];
        $return_time = array('is_end'=>0,'is_start'=>0,'first_day'=>0,'cansign'=>0,'last_day'=>0,'min_time'=>$min_time,'max_time'=>$max_time,'max_date'=>date("Y-m-d H:i:s",$max_time),'is_finish'=>0);

        if ((date('Ymd')-date('Ymd',$max_time))>=0 && (date('Ymd')-date('Ymd',$max_time))<=7) {
            $return_time['is_end'] = 1;
        }elseif ((date('YmdH')-date('YmdH',$min_time))<0) {
            $return_time['is_start'] = 1;
        }elseif ((date('Ymd')-date('Ymd',$max_time))>7) {
            $return_time['is_finish'] = 1;
        }
        foreach ($i_time as $key=>$val) {
            if (date('Y-m-d',$val)==date('Y-m-d')) {
                $now_hour = date("H");
                $activity_hour = date("H",$val);
                $diff_hour = $now_hour-$activity_hour;
                if ($key==0) {
                    $return_time['first_day'] = 1;
                }
                if ($diff_hour>-3 and $diff_hour<5){
                    $return_time['cansign'] = 1;
                }

                if (($key+1)==count($i_time)) {
                    $return_time['last_day'] = 1;
                }
            }
        }
        return ($return_time);
    }


    public function invitation($id) {
        $invite_object   = D('Admin/Invitation');
        $con['id']     = $id;
        $con['status'] = 1;
        $info          = $invite_object->where($con)->find();
        $time_info = $this->checkTime($info['atime']);
        $i_time = explode(",",$info['atime']);
        $this->assign('time_info',$time_info);

        $kol_object = D('Admin/Kol');
        $con['id'] = array('in',$info['brand_id']);
        $con['status'] = 1;
        $kol_info = $kol_object->where($con)->select();
        

        $info['tools'] = "<p>" . str_replace("\n", "</p><p>", $info['tools']) . "</p>";
        $info['step'] = "<p>" . str_replace("\n", "</p><p>", $info['step']) . "</p>";
 



        $user_model = D('Admin/User');
        $uid = $user_model->is_login();
        if ($uid) {
            $user_info = $user_model->find($uid);
            $user_model->auto_login($user_info);
            if ($user_info['price_id']>0) {
                $price_model = D('Admin/Price');
                $price_info = $price_model->find($user_info['price_id']);
                $this->assign('price_url',$price_info['price_url']);
            }

        }
        $this->assign('info',$info);

        $this->assign('i_time',$i_time);
        $this->assign('timestamp',time());
        $this->assign('kol_info',$kol_info);
        $this->assign('meta_title', $info['title']);
        $this->display('invitation');
    }

    public function orderInvite($id,$ch='intable') {
        $invite_object = D('Admin/Invitation');
        $con['id'] = $id;
        $con['status'] = 1;
        $info = $invite_object->where($con)->find();
        $inviteorder_object = D('Admin/Inviteorder');
        
        if ($info['id']) {
            if ($info['isprice']==0) {
                $info['price']=0;
            }
            $joindata = $inviteorder_object->getCount($id);
            $i_time = explode(",",$info['atime']);
            $this->assign('joindata',$joindata);
            $this->assign('timestamp',time());
            $this->assign('info',$info);
            $this->assign('ch',$ch);
            $this->assign('i_time',$i_time);
            $this->display('orderInvite');
        }else{
            $this->error('活动不存在');
        }
    }

    public function signInvite($id) {
        $invite_object = D('Admin/Invitation');
        $con['id'] = $id;
        $con['status'] = 1;
        $info = $invite_object->where($con)->find();

        if ($info['id']) {
            $this->assign('info',$info);
            $i_time = explode(",",$info['atime']);
            $this->assign('cansign',0);
            foreach ($i_time as $val) {
                if (date('Y-m-d',$val)==date('Y-m-d')) {
                    $this->assign('cansign',1);
                    break;
                }
            }
            $this->display('signInvite');
        }else{
            $this->error('活动不存在');
        }
    }



    public function checkSign() {
        $invite_object = D('Admin/Invitation');
        $id = intval($_POST['inviteid']);
        $con['id'] = $id;
        $con['status'] = 1;
        $info = $invite_object->where($con)->find();

        
        if ($info['id']) {
            $this->assign('info',$info);
            $i_time = explode(",",$info['atime']);
            $signdate = $cansign = 0;
            foreach ($i_time as $val) {
                if (date('Y-m-d',$val)==date('Y-m-d')) {
                    $cansign = 1;
                    $signdate = $val;
                    break;
                }
            }
            if ($cansign  == 1) {
                $order_object = D('Admin/Inviteorder');
                $ocon['mobile'] = $_POST['mobile'];
                $ocon['status'] = 1;
                $ocon['jointime'] = $signdate;
                $ocon['inviteid'] = $id;
                $oinfo = $order_object->where($ocon)->find();
                $this->assign('mobile',$_POST['mobile']);
                $this->assign('orderinfo',$oinfo);
                $this->display('checkSign');
            }else{
                $this->error('非有效签到时间');
            }
        }else{
            $this->error('活动不存在');
        }
    }

    public function jumpLive() {
        $invite_object = D('Admin/Invitation');
        $id = intval($_POST['inviteid']);
        
        $con['id'] = $id;
        $con['status'] = 1;
        $info = $invite_object->where($con)->find();

        
        if ($info['id']) {
            $this->assign('info',$info);
            $i_time = explode(",",$info['atime']);
            $signdate = $cansign = 0;
            foreach ($i_time as $val) {
                if (date('Y-m-d',$val)==date('Y-m-d')) {
                    $cansign = 1;
                    $signdate = $val;
                    break;
                }
            }
            if ($cansign  == 1) {
                $sign_object = D('Admin/Invitesign');
                $data = $sign_object->create(format_data());
                $data['jointime'] = $signdate;
                
                $order_object = D('Admin/Inviteorder');
                if ($_POST['orderid']>0) {
                    $ocon['id'] = intval($_POST['orderid']);
                    $ocon['mobile'] = $_POST['mobile'];
                    $ocon['inviteid'] = $id;
                    $ocon['jointime'] = $signdate;
                    $ocon['status'] = 1;
                    $oinfo = $order_object->where($ocon)->find();
                   
                    if (date('Y-m-d',$signdate)!=date('Y-m-d',$oinfo['jointime'])) {
                        $this->error('无效的签到');
                    }
                }
                
                $sid = $sign_object->add($data);
                if ($sid) {
                    $this->display('jumpSign');
                }else{
                    $this->error('签到失败');
                }
                
            }else{
                $this->error('非有效签到时间');
            }
        }else{
            $this->error('活动不存在');
        }
    }

    public function saveInvite() {


        $post_object = D('Admin/Inviteorder');
        
        $invite_object   = D('Admin/Invitation');

        $auth_object = D('Admin/Mobileauth');
        $data = $post_object->create(format_data());
        $mcon['mobile'] = $data['mobile'];
        $mcon['authcode'] = $_POST['authcode'];
        $mcon['status'] = 0;
        $checkauth  = $auth_object->where($mcon)->order('id desc')->find();
 
        if (!$checkauth['id']) {
            $this->error('验证码不正确');
        }else{
            $auth_object->where('id='.$checkauth['id'])->setInc('status');
        }

        $con['id']     = $data["inviteid"];
        $con['status'] = 1;
        $info          = $invite_object->where($con)->find();

        if ($info['id']) {
            $data['totalprice'] = $data['joinnum']*$info['price'];
            if ($info['isprice']==0) {
                $data['totalprice'] = 0;
            }
        }else{
            $this->error('活动不存在');
        }

        if ($data['jointime']<time()) {
            $this->error('该场次已结束，请选择其他场次');
        }
        $sparenum = $post_object->oneCount($data['inviteid'],$data['jointime']);
        if ($sparenum+$data['joinnum']>$info['num']) {
            $this->error('剩余名额不足，请减少购买数量或选择其他场次');
        }

        if ($return['Msg']) {
            echo (json_encode($return));
            exit;
        }
        if ($data) {
            $p_start = 0;
            
            $price_array = array();
            if ($info['s1_num'] && $info['s1_price']) {
                $price_array = array_merge($price_array,array_fill($p_start,$info['s1_num'],$info['s1_price']));
                $p_start +=$info['s1_num'];            
            }
            if ($info['s2_num'] && $info['s2_price']) {
                $price_array = array_merge($price_array,array_fill($p_start,$info['s2_num'],$info['s2_price']));
                $p_start +=$info['s2_num'];            
            }
            if ($info['s3_num'] && $info['s3_price']) {
                $price_array = array_merge($price_array,array_fill($p_start,$info['s3_num'],$info['s3_price']));
                $p_start +=$info['s3_num'];            
            }
            if ($info['s4_num'] && $info['s4_price']) {
                $price_array = array_merge($price_array,array_fill($p_start,$info['s4_num'],$info['s4_price']));
                //$p_start +=$info['s4_num'];            
            }
            $price_array = array_merge($price_array,array_fill($p_start,($info['num']-$info['s1_num']-$info['s2_num']-$info['s3_num']-$info['s4_num']),$info['price']));
     
            $now_price = array_slice($price_array,$sparenum,$data['joinnum']);
            foreach ($now_price as $val) {
                $data['realprice'] += $val;
            }
            $data['price_data'] = serialize($now_price);
             
            if ($info['is_price']==0) {
                $data['realprice'] = 0;
            }

            if ($data['realprice']==0) {
                $data['ispay'] = 1;
            }else{
                $data['ispay'] = 0;
            }


            $data['ch'] = $_POST['ch'];
            $data['transaction_id'] = '';
            $data['out_trade_no'] = '';
            $data['openid'] = '';
            $id = $post_object->add($data);
            
            if ($id) {
                if ($data['realprice']==0) {
                    send_sms($data['mobile'],2,$id,0);
                    $this->redirect('invitation',array('id'=>$info['id']));
                    //$this->success('预约成功', U('invitation',array('id'=>$info['id'])));
                }else{
                    $pay_info['payorderid'] = md5('intable'.$id);
                    $post_object->where('id='.$id)->data($pay_info)->save();
                    $this->redirect('Wxpay/pay',array('payid'=>$pay_info['payorderid']));
                    //$this->success('预约成功，完成支付立即生效',U('Wxpay/pay',array('payid'=>$pay_info['payorderid'])));
                }
                
            }else{
                $this->error('预约失败');
            }
    } else {
            $this->error('预约失败');
        }
        
    }

    

    public function getInvnum($id ='',$time = '') {
        $post_object = D('Admin/Inviteorder');
        $sparenum = $post_object->oneCount($id,$time);
        if (!$sparenum) {
            $sparenum = 0;
        }
        echo $sparenum;
    }

    public function kolinfo($id) {
        $post_object   = D('Admin/Kol');
        $con['id']     = $id;
        $con['status'] = 1;
        $info          = $post_object->where($con)->find();
        $info['intro'] = "<p>" . str_replace("\n", "</p><p>", $info['intro']) . "</p>";
        unset($con);
        $inv_object = D('Admin/Invitation');
        $con['_string'] = 'FIND_IN_SET('.$id.',brand_id)';
        //$con['brand_id'] = $id;
        $con['status'] = 1;
        $inv_list = $inv_object->where($con)->select();
        
        $this->assign('inv_list',$inv_list);
        $this->assign('info', $info);
        
        $this->assign('meta_title', $info['title']);
        $this->display('kolInfo');
    }

    function upload()
    {
        $this->display('upload');
    }


    /**
     * 系统配置
     * @author jry <598821125@qq.com>
     */
    public function config($name = '')
    {
        $data_list = C($name);
        $this->assign('data_list', $data_list);
        $this->assign('meta_title', '系统配置');
        $this->display();
    }

    /**
     * 导航
     * @author jry <598821125@qq.com>
     */
    public function nav($group = 'wap_bottom')
    {
        $data_list = D('Admin/Nav')->getNavTree(0, $group);
        $this->assign('data_list', $data_list);
        $this->assign('meta_title', '导航列表');
        $this->display();
    }

    public function saveform() {
        $post_object = D('Admin/Order');
        $data = $post_object->create(format_data());
        if ($data) {
            $id = $post_object->add();
            if ($id) {
                //$this->success('新增成功', U('post', array('id' => $data['post_id'])));
                $this->success('新增成功', U('order'));
            } else {
                $this->error('新增失败');
            }
        } else {
            $this->error($post_object->getError());
        }
    }

    /**
     * 模块
     * @author jry <598821125@qq.com>
     */
    public function module()
    {
        $map['status'] = 1;
        $data_list     = D('Admin/MODULE')->where($map)->select();
        $this->assign('data_list', $data_list);
        $this->assign('meta_title', '模块列表');
        $this->display();
    }
    
    public function intablesms($mobile) {
        if(!preg_match("/1[0-9]{10}/",$mobile))
        {
            echo '手机号不正确';
            exit;
        }
        send_sms($mobile,4);
    }
    
}
